#include<iostream>
#include<vector>
#include<map>

class Solution {
public:
//一层哈希
    std::vector<int> twoSum_hashone(std::vector<int>& nums, int target) 
    {
        std::vector<int> res(2, -1);//输出
        std::map<int, int> a;
        for(size_t i = 0; i < nums.size(); ++i)
        {
           if(a.count(target - nums[i]) >0 && i != a[target - nums[i]])
           {
                res[0] = i;
                res[1] = a[target - nums[i]];
                return res;
           }
           else
           {
               a.insert(std::map<int, int>::value_type(nums[i], i));
               // a[nums[i]] = i;
           }
        }
       
        return res;
    }
    //两层哈希
    std::vector<int> twoSum_hash(std::vector<int>& nums, int target) 
    {
        std::vector<int> res(2, -1);//输出
        std::map<int, int> a;
        for(size_t i = 0; i < nums.size(); ++i)
        {
           a.insert(std::map<int, int>::value_type(nums[i], i));
        }
        for(size_t i = 0; i < nums.size(); ++i)
        {
            int complement = target - nums[i];
            auto it = a.find(complement);    
            if(a.count(target - nums[i]) > 0 && a[target - nums[i]] != i) //找到且不是同一个元素
            {
                res[0] = i;
                res[1] = a[target - nums[i]];
                //std::cout << '[' << res[0] << ',' << res[1] << ']' << std::endl;
                break;
            }
        }
        return res;
    }
    //暴力解法
    std::vector<int> twoSum(std::vector<int>& nums, int target) 
    {
        std::vector<int> res;
        for(size_t i = 0; i < nums.size(); ++i)
        {
            for(size_t j = i+1; j < nums.size(); ++j)
            {
                if(nums[i] + nums[j] == target)
                {
                    //std::cout << '[' << i << ',' << j << ']';
                    res.push_back(i);
                    res.push_back(j);
                    return res;
                }
            }
        }
        return res;
    }
};

int main(void)
{
  Solution s;
  std::vector<int> nums = {2,5,5,11};
  std::vector<int> res;
  int target(10);
  res = s.twoSum_hashone(nums, target);
  std::cout<<'[';
  for(size_t i = 0; i < res.size(); ++i)
  {
    std::cout << res[i] << ',';
  }
  std::cout<<']';
}